﻿// ע�

using IotClientService.Models.Lg;
using IotClientService.Services;
using IotContract.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Common.Extensions;

namespace IotClientService.Handles.lgs;
public class LgHandlePreCharge : LgHandleFmt
{
    public LgHandlePreCharge(ILgApiService lgCallApiService) : base(lgCallApiService)
    {
    }
    public override IotEquipType EquipType { get; set; } = IotEquipType.PRECHARGE;

    /// <summary>
    /// Tray Load报告
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="replyCmd"></param>
    public async override Task Handle933(LgMsgInfo msg, string replyCmd)
    {
        var sendMsg = new LgMsgInfo
        {
            Direction = 0,
            ObjID = msg.ObjID,
            HasMsg = 0,
            SeqNo = msg.SeqNo,
            Command = replyCmd,

        };

        
        var trayId = msg.Message.Substring(18, 20).Replace(" ","");
        var status = msg.Message.Substring(38, 1);

        var result = await Api.CommitPreChargeEnterOut(new Models.Lg.Remote.LgTrayReportRequest
        {
            EquipNum = Equip.Num,
            EquipType = this.EquipType,
            ProgramType = Equip.ProgramType,
            Tray = trayId,
            Status = status
        });
        int ret = 0;
        if (!result.Success)
        {
            Equip.ExMessage = result.Message;
            ret = 1;
        }
        else
        {
            ret = result.Result?.Ret??1;
        }
        if(ret == 1)
        {
            msg.Body = ret.ToString();
            await Handle981(msg, "981");
            return;
        }
        #region 传输body
        // 0:ok 1:ng  
        
        string ngCode = ((result.Result?.NgCode)??"0").PadLeft(4, '0');
        
       
        string cells = string.Empty;
        if (ret == 0)
        {
            for (int i = 1; i <= 48; i++)
            {
                var cell = result.Result.Cells.FirstOrDefault(x => x.channel == i);
                if (cell == null)
                {
                    cells += "".PadRight(24, ' ');
                }
                else
                {
                    cells +=  cell.cellCode.PadRight(24, '0');
                }
            }
        }
        //cells = cells.PadRight(24 * 48, ' ');
        // M11__ 产品型号
        var model = (result.Result?.Prod??"").PadRight(10, ' ');
       
        #endregion
        sendMsg.Body = $"{ret}{ngCode}{cells}{model}";
        sendMsg.IsSend = true;
        InvokeSend(sendMsg);
    }

    /// <summary>
    /// Tray UnLoad报告
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="replyCmd"></param>
    public override async Task Handle937(LgMsgInfo msg, string replyCmd)
    {
        var sendMsg = new LgMsgInfo
        {
            Direction = 0,
            ObjID = msg.ObjID,
            HasMsg = 0,
            SeqNo = msg.SeqNo,
            Command = replyCmd,

        };
        // 托盘
        var tray = msg.Message.Substring(18, 10);
        // E:End完成 R:Request(Unload邀请) O:Open Jig(化成用)
        var status = msg.Message.Substring(38, 1);


        Equip.Obj = "";
        Equip.ExplainData = status;

        var result = await Api.CommitPreChargeEnterOut(new Models.Lg.Remote.LgTrayReportRequest
        {
            EquipNum = Equip.Num,
            EquipType = this.EquipType,
            ProgramType = Equip.ProgramType,
            Tray = tray,
            Status = status
        });
        // 0:ok 1:ng,再传送后trouble
        int ret = 0;
        if (!result.Success)
        {
            Equip.ExMessage = result.Message;
            ret = 1;
        }
        else
        {
            ret = result.Result?.Ret ?? 1;
        }
        if (ret == 1)
        {
            msg.Body = ret.ToString();
            await Handle981(msg, "981");
            return;
        }
        Equip.Remark = $"tray unload:{tray}-{status}";
        sendMsg.Body = $"{ret}";
        sendMsg.IsSend = true;
        InvokeSend(sendMsg);
    }
}
